﻿#include "precomp.h"
#include "jthreadpool.h"
#include "private/jthreadpool_p.h"
//#include "private/qthreadpool_p.h"

// - class JThreadPoolPrivate -

/**
 * @brief JThreadPoolPrivate::inited
 */
bool JThreadPoolPrivate::inited = false;

// - class JThreadPoolEx -

/**
 * @brief JThreadPoolEx::JThreadPoolEx
 * @param parent
 */
JThreadPoolEx::JThreadPoolEx(QObject *parent)
    : QThreadPool(parent)
    , _d(0)
{
    //_d = reinterpret_cast<QThreadPoolPrivate *>(d_ptr.data());
}

// - class JThreadPoolPrivate -

/**
 * @brief JThreadPoolPrivate::stealRunnable
 * @param runnable
 */
void JThreadPoolPrivate::stealRunnable(JRunnable *runnable)
{
    J_UNUSED(runnable);
    if (threadPool->inherits("JThreadPoolEx")) {
        qDebug() << "stealRunnable";
        QThreadPoolPrivate *_d = qobject_cast<JThreadPoolEx *>(threadPool)->_d;
        if (_d) {
            //_d->stealRunnable(reinterpret_cast<QRunnable *>(runnable));
        }
    }
}

// - class JThreadPool -

/**
 * @brief JThreadPool::JThreadPool
 */
JThreadPool::JThreadPool()
    : d_ptr(new JThreadPoolPrivate(this))
{

}

/**
 * @brief JThreadPool::JThreadPool
 * @param other
 */
JThreadPool::JThreadPool(QThreadPool *other)
    : d_ptr(new JThreadPoolPrivate(this, other))
{

}

/**
 * @brief JThreadPool::~JThreadPool
 */
JThreadPool::~JThreadPool()
{
    delete d_ptr;
}

/**
 * @brief JThreadPool::globalInstance
 * @return
 */
JThreadPool JThreadPool::globalInstance()
{
    return JThreadPool(QThreadPool::globalInstance());
}

/**
 * @brief JThreadPool::start
 * @param runnable
 * @param priority
 */
void JThreadPool::start(JRunnable *runnable, int priority)
{
    J_D(JThreadPool);
    return d->threadPool->start(reinterpret_cast<QRunnable *>(runnable), priority);
}

/**
 * @brief JThreadPool::tryStart
 * @param runnable
 * @return
 */
bool JThreadPool::tryStart(JRunnable *runnable)
{
    J_D(JThreadPool);
    return d->threadPool->tryStart(reinterpret_cast<QRunnable *>(runnable));
}

/**
 * @brief JThreadPool::expiryTimeout
 * @return
 */
int JThreadPool::expiryTimeout() const
{
    J_D(const JThreadPool);
    return d->threadPool->expiryTimeout();
}

/**
 * @brief JThreadPool::setExpiryTimeout
 * @param expiryTimeout
 */
void JThreadPool::setExpiryTimeout(int expiryTimeout)
{
    J_D(JThreadPool);
    d->threadPool->setExpiryTimeout(expiryTimeout);
}

/**
 * @brief JThreadPool::maxThreadCount
 * @return
 */
int JThreadPool::maxThreadCount() const
{
    J_D(const JThreadPool);
    return d->threadPool->maxThreadCount();
}

/**
 * @brief JThreadPool::setMaxThreadCount
 * @param maxThreadCount
 */
void JThreadPool::setMaxThreadCount(int maxThreadCount)
{
    J_D(JThreadPool);
    d->threadPool->setMaxThreadCount(maxThreadCount);
}

/**
 * @brief JThreadPool::activeThreadCount
 * @return
 */
int JThreadPool::activeThreadCount() const
{
    J_D(const JThreadPool);
    return d->threadPool->activeThreadCount();
}

/**
 * @brief JThreadPool::reserveThread
 */
void JThreadPool::reserveThread()
{
    J_D(JThreadPool);
    d->threadPool->reserveThread();
}

/**
 * @brief JThreadPool::releaseThread
 */
void JThreadPool::releaseThread()
{
    J_D(JThreadPool);
    d->threadPool->releaseThread();
}

/**
 * @brief JThreadPool::waitForDone
 */
void JThreadPool::waitForDone()
{
    J_D(JThreadPool);
    d->threadPool->waitForDone();
}

/**
 * @brief JThreadPool::waitForDone
 * @param msecs
 * @return
 */
bool JThreadPool::waitForDone(int msecs)
{
    J_D(JThreadPool);
    return d->threadPool->waitForDone(msecs);
}
